import pygame
import sys
import sys
import math
from enum import Enum

# 初始化pygame
pygame.init()

# 屏幕设置
WIDTH, HEIGHT = 400, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("电子宠物 Demo")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)


# 宠物状态枚举
class PetState(Enum):
    HAPPY = 0
    HUNGRY = 1
    SLEEPY = 2
    SICK = 3
    DIRTY = 4


# 宠物类
class VirtualPet:
    def __init__(self, name):
        self.name = name
        self.hunger = 50
        self.happiness = 50
        self.energy = 50
        self.cleanliness = 50
        self.health = 100
        self.age = 0  # 天数
        self.state = PetState.HAPPY
        self.is_sleeping = False
        self.animation_frame = 0
        self.last_update_time = pygame.time.get_ticks()

    def update(self):
        current_time = pygame.time.get_ticks()
        if current_time - self.last_update_time > 1000:  # 每秒更新一次
            self.hunger = min(100, max(0, self.hunger - 1))
            self.happiness = min(100, max(0, self.happiness - 0.5))
            self.cleanliness = min(100, max(0, self.cleanliness - 0.3))

            if self.is_sleeping:
                self.energy = min(100, self.energy + 2)
                if self.energy >= 80:
                    self.is_sleeping = False
            else:
                self.energy = min(100, max(0, self.energy - 0.5))

            # 健康度受其他属性影响
            if self.hunger < 20 or self.cleanliness < 20 or self.happiness < 20:
                self.health = max(0, self.health - 1)
            else:
                self.health = min(100, self.health + 0.5)

            # 更新状态
            if self.health < 50:
                self.state = PetState.SICK
            elif self.hunger < 30:
                self.state = PetState.HUNGRY
            elif self.energy < 30 and not self.is_sleeping:
                self.state = PetState.SLEEPY
            elif self.cleanliness < 30:
                self.state = PetState.DIRTY
            else:
                self.state = PetState.HAPPY

            self.age += 1 / 60  # 每分钟增加1天
            self.animation_frame = (self.animation_frame + 1) % 60
            self.last_update_time = current_time

    def feed(self):
        if not self.is_sleeping:
            self.hunger = min(100, self.hunger + 20)
            self.happiness = min(100, self.happiness + 5)

    def play(self):
        if not self.is_sleeping and self.energy > 10:
            self.happiness = min(100, self.happiness + 15)
            self.energy = max(0, self.energy - 10)
            self.hunger = max(0, self.hunger - 5)

    def clean(self):
        self.cleanliness = min(100, self.cleanliness + 30)
        self.happiness = min(100, self.happiness + 5)

    def sleep(self):
        self.is_sleeping = not self.is_sleeping

    def medicine(self):
        self.health = min(100, self.health + 30)

    def draw(self, surface):
        # 绘制宠物
        pet_size = 120
        pet_x = WIDTH // 2
        pet_y = HEIGHT // 2 - 50

        # 根据状态改变颜色
        if self.state == PetState.HAPPY:
            color = YELLOW
        elif self.state == PetState.HUNGRY:
            color = (255, 165, 0)  # 橙色
        elif self.state == PetState.SLEEPY:
            color = BLUE
        elif self.state == PetState.SICK:
            color = (200, 0, 0)  # 暗红色
        elif self.state == PetState.DIRTY:
            color = (139, 69, 19)  # 棕色

        # 绘制宠物身体
        pygame.draw.circle(surface, color, (pet_x, pet_y), pet_size // 2)

        # 如果是睡觉状态，绘制闭上的眼睛
        if self.is_sleeping:
            pygame.draw.arc(surface, BLACK,
                            (pet_x - 30, pet_y - 20, 60, 40),
                            0, 3.14, 2)
        else:
            # 绘制眼睛
            eye_size = 15 + 5 * math.sin(self.animation_frame * 0.1)
            pygame.draw.circle(surface, BLACK, (pet_x - 25, pet_y - 15), eye_size // 2)
            pygame.draw.circle(surface, BLACK, (pet_x + 25, pet_y - 15), eye_size // 2)

            # 绘制嘴巴
            mouth_happy = 10 * math.sin(self.animation_frame * 0.2)
            pygame.draw.arc(surface, BLACK,
                            (pet_x - 20, pet_y - 10 + mouth_happy, 40, 30),
                            0.2, 2.9, 2)

        # 绘制状态文本
        font = pygame.font.SysFont(None, 24)
        state_text = font.render(f"statu: {self.state.name}", True, BLACK)
        surface.blit(state_text, (20, 20))

        # 绘制属性条
        self.draw_stat_bar(surface, 20, 50, "hunger", self.hunger, RED)
        self.draw_stat_bar(surface, 20, 80, "happy", self.happiness, GREEN)
        self.draw_stat_bar(surface, 20, 110, "energy", self.energy, BLUE)
        self.draw_stat_bar(surface, 20, 140, "clean", self.cleanliness, (0, 200, 200))
        self.draw_stat_bar(surface, 20, 170, "health", self.health, (0, 200, 0))

        # 绘制年龄
        age_text = font.render(f"age: {int(self.age)}天", True, BLACK)
        surface.blit(age_text, (20, 200))

    def draw_stat_bar(self, surface, x, y, label, value, color):
        font = pygame.font.SysFont(None, 24)
        label_text = font.render(f"{label}:", True, BLACK)
        surface.blit(label_text, (x, y))

        bar_width = 200
        bar_height = 20
        filled_width = int(bar_width * (value / 100))

        pygame.draw.rect(surface, GRAY, (x + 80, y, bar_width, bar_height))
        pygame.draw.rect(surface, color, (x + 80, y, filled_width, bar_height))
        pygame.draw.rect(surface, BLACK, (x + 80, y, bar_width, bar_height), 2)

        value_text = font.render(f"{int(value)}%", True, BLACK)
        surface.blit(value_text, (x + 80 + bar_width + 10, y))


# 按钮类
class Button:
    def __init__(self, x, y, width, height, text, color, hover_color):
        self.rect = pygame.Rect(x, y, width, height)
        self.text = text
        self.color = color
        self.hover_color = hover_color
        self.is_hovered = False
        self.font = pygame.font.SysFont(None, 28)

    def draw(self, surface):
        color = self.hover_color if self.is_hovered else self.color
        pygame.draw.rect(surface, color, self.rect)
        pygame.draw.rect(surface, BLACK, self.rect, 2)

        text_surface = self.font.render(self.text, True, BLACK)
        text_rect = text_surface.get_rect(center=self.rect.center)
        surface.blit(text_surface, text_rect)

    def check_hover(self, pos):
        self.is_hovered = self.rect.collidepoint(pos)
        return self.is_hovered

    def is_clicked(self, pos, event):
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            return self.rect.collidepoint(pos)
        return False


# 创建宠物和按钮
pet = VirtualPet("小P")
buttons = [
    Button(20, HEIGHT - 150, 80, 40, "feed", (255, 200, 200), (255, 150, 150)),
    Button(120, HEIGHT - 150, 80, 40, "play", (200, 255, 200), (150, 255, 150)),
    Button(220, HEIGHT - 150, 80, 40, "clean", (200, 200, 255), (150, 150, 255)),
    Button(320, HEIGHT - 150, 80, 40, "sleep", (255, 255, 200), (255, 255, 150)),
    Button(20, HEIGHT - 100, 80, 40, "hospital", (255, 200, 255), (255, 150, 255)),
    Button(120, HEIGHT - 100, 80, 40, "reset", (200, 200, 200), (150, 150, 150))
]

# 游戏主循环
clock = pygame.time.Clock()
running = True

while running:
    mouse_pos = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # 检查按钮点击
        for button in buttons:
            if button.is_clicked(mouse_pos, event):
                if button.text == "feed":
                    pet.feed()
                elif button.text == "play":
                    pet.play()
                elif button.text == "clean":
                    pet.clean()
                elif button.text == "sleep":
                    pet.sleep()
                elif button.text == "hospital":
                    pet.medicine()
                elif button.text == "reset":
                    pet = VirtualPet("PP")

    # 更新宠物状态
    pet.update()

    # 检查按钮悬停
    for button in buttons:
        button.check_hover(mouse_pos)

    # 绘制
    screen.fill(WHITE)
    pet.draw(screen)

    for button in buttons:
        button.draw(screen)

    # 显示帮助文本
    font = pygame.font.SysFont(None, 24)
    help_text = font.render("start game!", True, BLACK)
    screen.blit(help_text, (20, HEIGHT - 50))

    pygame.display.flip()
    clock.tick(60)

pygame.quit()
sys.exit()